using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Globalization;
using Microsoft.WowAddonStudio.FrameXml.Design.Services.NameCreationService;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Tests
{
	/// <summary>
	/// 
	/// </summary>
	public class TestNameCreationService : INameCreationService
	{
		private readonly DataTypeMapper dataTypeMapper = new DataTypeMapper();

		#region INameCreationService Members

		/// <summary>
		/// Creates a new name that is unique to all components in the specified container.
		/// </summary>
		/// <param name="container">The container where the new object is added.</param>
		/// <param name="dataType">The data type of the object that receives the name.</param>
		/// <returns>A unique name for the data type.</returns>
		public string CreateName(IContainer container, Type dataType)
		{
			if (dataType == null)
				throw new ArgumentNullException("dataType");

			string result;
			try
			{
				var context = new UniqueNumberGeneratorContext(new SimpleUniqueNumberGenerator(container));
				string dataTypeName = dataTypeMapper[dataType].Name;

				result = String.Format(CultureInfo.InvariantCulture, "{0}{1}",
									   dataTypeName, context.Generator.GenerateUniqueNumber(dataTypeName));
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex);
				result = String.Format("{0}_{1:B}", dataType.Name, Guid.NewGuid());
			}

			return result;
		}

		/// <summary>
		/// Gets a value indicating whether the specified name is valid.
		/// </summary>
		/// <param name="name">The name to validate.</param>
		/// <returns>
		/// true if the name is valid; otherwise, false.
		/// </returns>
		public bool IsValidName(string name)
		{
			return true;
		}

		/// <summary>
		/// Gets a value indicating whether the specified name is valid.
		/// </summary>
		/// <param name="name">The name to validate.</param>
		public void ValidateName(string name)
		{
			IsValidName(name);
		}

		#endregion
	}
}